package com.example.dsplatform.Service.ipml;

import cn.hutool.core.util.RandomUtil;
import com.example.dsplatform.Dao.Mapper.MenuMapper;
import com.example.dsplatform.Dao.Mapper.UserMapper;
import com.example.dsplatform.Dao.Mapper.User_RoleMapper;
import com.example.dsplatform.Service.AdminService;
import com.example.dsplatform.pojo.Entity.ResponseResult;
import com.example.dsplatform.pojo.Entity.Code;
import com.example.dsplatform.pojo.Entity.User;
import com.example.dsplatform.utls.RedisCache;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class AdminServiceIpml implements AdminService {
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    User_RoleMapper userRoleMapper;
    @Autowired
    RedisCache redisCache;



    public ResponseResult insert(User user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        String passworden = passwordEncoder.encode(password);
        user.setPassword(passworden);
        /*
        如果不能跑，记得是这个地方改了，原来是int result =
        */

        userMapper.insert(user);
        return new ResponseResult(200, "注册成功");

    }

    public ResponseResult delete(User user) {
        /*
        如果不能跑，记得是这个地方改了，原来是int result =
        */

        userMapper.deleteById(user.getId());
        return new ResponseResult(200, "删除成功");

    }

    public ResponseResult selectByName(User user) {
        List<String> result = (List<String>) userMapper.selectByName(user.getUsername());
        return new ResponseResult(200, "查询成功",result);

    }



    public ResponseResult selecttable() {
        List<User> result = userMapper.selectList(null);
        return new ResponseResult(200,"查询成功",result);
    }

    public ResponseResult selecttablePage(int current_Page,int pageSize) {
        PageHelper.startPage(current_Page,pageSize);
        List<User> result = userMapper.selectList(null);
        return new ResponseResult(200,"查询成功",result);
    }


    //todo 把所有有权限的人列出来
    public ResponseResult selectUserByPerm(String perm) {
        List<String> result = userMapper.selectUserByPerm(perm);
        return new ResponseResult(200, result);
    }

    public ResponseResult updateById(User user) {
        userMapper.updateById(user);
        return new ResponseResult(200, "修改成功");
    }


    public ResponseResult selectUser_Role() {
        List<String> result =  userMapper.selectUser_Role();
        return new ResponseResult(200,"查看成功", result);
    }



    public  ResponseResult selectByUid(User user){
        List<String> result =  userMapper.selectByUID(user.getId());
        return new ResponseResult(200,"查看成功", result);

    }

    @Override
    public ResponseResult sentEmailCode(String id,String email) {
        Code code = new Code();
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("hook3004@sina.com");
        code.setUid(id);
        code.setData(RandomUtil.randomNumbers(4));
        code.setEmail(email);
        message.setSubject("邮箱验证");
        message.setText("[学生管理系统]你的验证码为："+code.getData()+"，请妥善保管，切勿告诉外人");
        message.setTo(email);
        javaMailSender.send(message);
        redisCache.setCacheObject("Code:"+code.getData(), code,5, TimeUnit.MINUTES);
        return new ResponseResult(200, "验证码已发送");
    }

    @Override
    public ResponseResult loginByCode(String email,String code) {


        Code codeE = redisCache.getCacheObject("Code:"+code);
        if(Objects.isNull(codeE)){
            return new ResponseResult(204, "验证不通过");
        }
        else if (email.equals(codeE.getEmail()) && code.equals(codeE.getData())) {
            String id = codeE.getUid();
            userMapper.updateEmail(id,email);
            return new ResponseResult(200, "通过");


        }
        else  {
            return new ResponseResult(204, "验证不通过");
        }


    }

}
